const db = wx.cloud.database()
var uid = "41ae62ef621f180f09e5823e6f35ac1a";
var i = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartId: "",
    cart: [],
  },

  async getData() {
    var pids = [];
    var num = [];
    var cart = [];
    var cartId = "";
    var that = this;
    var i = 0
    await db.collection("cart").where({
        uid: uid
      })
      .get().then(res => {
        cartId = res.data[0]._id;
        // console.log(res.data[0].pids)
        res.data[0].pids.forEach(item => {
          // console.log(item.id)
          pids.push(item.id)
          // console.log("pids=" + pids)
          num.push(item.num)
          // console.log("num=" + num)
        })
        that.setData({
          cartId: cartId
        })
      })
      console.log("pids="+pids)
     
    pids.forEach(async item => {
      console.log(item)
      wx.cloud.callFunction({
        name: "getProforCart",
        data:{
          item:item
        }
      })
      .then(res => {
          console.log("res="+res)
          var {
            _id,
            pname,
            picUrl,
            price
          } = res.data[0];
          cart.push({
            _id,
            pname,
            picUrl,
            price,
            num: num[i],
            no: i,
          })
          console.log("i="+i)
          i = i + 1;
          console.log("i="+i)
          console.log("CART="+cart)
          that.setData({
            cart
          })
        })
    })
  },

  async addData(options) {
    var that = this;
    var num = 0;
    var no = 0;
    console.log("that.data.cart="+that.data.cart)
    var cart = that.data.cart;
    console.log("cart="+cart)
    console.log("得到的num="+options.currentTarget.dataset.num)
    console.log("得到的no="+options.currentTarget.dataset.no)
    num = options.currentTarget.dataset.num;
    no = options.currentTarget.dataset.no;
    no = no - 1;
    console.log("num="+num)
    console.log("no="+no)
    num = num + 1;
    console.log("旧的num="+cart[no])
    cart[no].num = num;
    console.log("新的num="+cart[no].num)
    console.log("cart="+cart)
    that.setData({
      cart
    })

    // 修改数据库
    var cartId = "";
    cartId = that.data.cartId;
    var id = that.data.cart[no]._id
    console.log(id)
    // 更新购物车云函数
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.cloud.callFunction({
      name: "update",
      data: {
        num: num,
        no: no,
        cartId: cartId,
        id: id,
      },
      success: function (res) {
        console.log(res.result.event)
      },
      fail: console.error
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)
  },

  subData(options) {
    var that = this;
    var num = 0;
    var no = 0;
    var cart = that.data.cart;
    // console.log("得到的num="+options.currentTarget.dataset.num)
    // console.log("得到的no="+options.currentTarget.dataset.no)
    num = options.currentTarget.dataset.num;
    no = options.currentTarget.dataset.no;
    num = num - 1;
    // console.log("旧的num="+cart[no].num)
    cart[no].num = num;
    // console.log("新的num="+cart[no].num)
    // console.log("cart="+cart)
    that.setData({
      cart
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.callFunction({
      name: "getProforCart",
      data:{
        id:"17e3426e621d6a07113b541d64a3e5a3"
      }
    }).then(res=>{
      console.log("rerere"+res)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})